package com.wiserun.RestFul.service;

import com.wiserun.RestFul.mapper.PncsOrganizationMapper;
import com.wiserun.RestFul.mapper.PncsUserRecordMapper;
import com.wiserun.RestFul.model.User;
import com.wiserun.RestFul.model.UserRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PncsUserRecordService {

    @Autowired
    private PncsUserRecordMapper pncsUserRecordMapper;

    //增加单个用户任职记录
    public int addUserRecord(UserRecord userRecord) {
        int num = 0;
        if (userRecord != null) {
            num = pncsUserRecordMapper.addUserRecord(userRecord);
        }
        return num;
    }

    //增加多条用户任职记录
    public HashMap<String,Integer> addUserRecord(List<UserRecord> userRecords) {
        int num_add = 0;
        int num_update = 0;
        int sum_add = 0;
        int sum_update = 0;
        HashMap<String,Integer> counts =new HashMap<>();
        if (userRecords != null) {
            //System.out.println("获取的userrecord数据是========="+userRecords);
            for(UserRecord userRecord: userRecords){
                //去除重复 count>=1 表示有重复数据
                int count = pncsUserRecordMapper.countUserRecord(userRecord);
                if(count<1){//没有重复数据
                    num_add = pncsUserRecordMapper.addUserRecord(userRecord);
                    sum_add+= num_add;
                    continue;
                }
                num_update = pncsUserRecordMapper.updateUserRecord(userRecord);//修改数据
                sum_update+=num_update;
            }
        }
        counts.put("sum_add",sum_add);
        counts.put("sum_update",sum_update);
        return counts;
    }

}
